package com.frxuexi.park2305

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.frxuexi.lib_base.entity.NewsBean
import com.frxuexi.lib_base.http.ApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class MainViewModel @Inject constructor(val apiService: ApiService):ViewModel() {

    val newsData by lazy { MutableLiveData<NewsBean>() }
    fun getNews(){
        viewModelScope.launch {
            apiService.getNews().flowOn(Dispatchers.IO).collect{
                if(it.code==200){
                    newsData.postValue(it.data)
                }
            }
        }
    }
}